//
//  BasicConfigurationService.m
//  XTCloud
//
//  Created by QA on 2017/11/27.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "BasicConfigurationService.h"
#import "WebServiceCommonRequest.h"
#import "NSDictionary+DataFormat.h"
#import "BasicConfigurationHelper.h"

static BasicConfigurationService *sharedMenuService = nil;
@implementation BasicConfigurationService

+ (BasicConfigurationService*)sharedService
{
    @synchronized(self){
        if (sharedMenuService == nil){
            sharedMenuService = [[self alloc] init];
        }
    }
    return sharedMenuService;
}


//删除配置文件
- (void)clearBasicConfigurationData
{
    [BasicConfigurationHelper delectConfigurationData];
}

#pragma mark - 网络获取站点
- (void)startQueryService
{
    //删除配置文件
    [BasicConfigurationHelper delectConfigurationData];
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"OTHER_BASIC_CONFIGURATION"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        //添加数据，刷新页面
        if(result.resultArray && result.resultArray.count>0){
            NSArray *resultValue = result.resultArray.firstObject;
            if(resultValue.count <= 0) return;
            NSDictionary *dictionary = resultValue[0];
            
            BasicConfigurationCodeing *kBasicInfo = [[BasicConfigurationCodeing alloc] init];
            kBasicInfo.startDayTime = [dictionary objectFormatStringForKey:@"日开始时间"];
            kBasicInfo.endDayTime = [dictionary objectFormatStringForKey:@"日结束时间"];
            kBasicInfo.startMonthTime = [dictionary objectFormatStringForKey:@"月开始时间"];
            kBasicInfo.endMonthTime = [dictionary objectFormatStringForKey:@"月结束时间"];
            
            //保存用户登录信息
            [BasicConfigurationHelper saveConfigurationData:kBasicInfo];
        }
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        //延迟5秒，重新查询数据
        __block BasicConfigurationService *weakSelf = self;
        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC));
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            [weakSelf startQueryService];
        });
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        //延迟5秒，重新查询数据
        __block BasicConfigurationService *weakSelf = self;
        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC));
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            [weakSelf startQueryService];
        });
    }];
}

@end
